* ==============================================================================
* ==============================================================================
* IMF Book: IMFBOOK_Ch5_Code.do
*			Chapter 5: Conditionality and income inequality
* ==============================================================================
use "IMFBOOK_Dataset.dta", clear
* ==============================================================================
* Generate variables
* ==============================================================================
encode ccode, gen(cid)
xtset cid year
* DVs
rename gini_disp_SOLT gini_disp
foreach n of numlist 1/10 {
	gen incdec`n' = share`n'_GID_GCIP*100
}
* Controls
rename sp_urb_totl_in_zs_WDI urban
rename sp_pop_dpnd_WDI dratio
rename ny_gdp_pcap_kd_WDI gdppc
gen gdppc2 = gdppc^2
gen lngdppc = ln(gdppc)
gen lngdppc2 = lngdppc^2
rename ne_trd_gnfs_zs_WDI trade
rename bx_klt_dinv_wd_gd_zs_WDI fdi
rename fh_ipolity2_QOG democracy
by cname, sort: ipolate democracy year, generate(demo2)
* IMF selection
rename bca_ngdpd_WEO cab
gen IMFliq = ln(IMFliquidity)
* EVs
egen IMFunder = sum(IMF55), by(year)
* propensity for country to recieve conditions
egen varBA2TOT=mean(BA2TOT) if year>=1980 & year<=2017, by(cid)
gen intBA2TOT=varBA2TOT*IMFliq
gen SCsBA2 = SPCsTOT + PAsTOT
gen QCsBA2 = QPCsTOT
egen varSCsBA2=mean(SCsBA2) if year>=1980 & year<=2017, by(cid)
gen intSCsBA2=varSCsBA2*IMFliq
egen varQCsBA2=mean(QCsBA2) if year>=1980 & year<=2017, by(cid)
gen intQCsBA2=varQCsBA2*IMFliq
gen BA2RFP = BA2FP + BA2RTP
gen BA2ENT = BA2SOE + BA2PRI
gen BA2GOV = BA2ENV + BA2INS + BA2OTH + BA2SP
foreach cond in BA2DEB BA2ENV BA2EXT BA2FIN BA2FP BA2INS BA2LAB BA2OTH BA2POV BA2PRI BA2RTP BA2SOE BA2SP BA2RFP BA2ENT BA2GOV {
	egen var`cond'=mean(`cond') if year>=1980 & year<=2017, by(cid)
	gen int`cond'=var`cond'*IMFliq
	gen res`cond' = BA2TOT - `cond'
	egen varres`cond'=mean(res`cond') if year>=1980 & year<=2017, by(cid)
	gen intres`cond'=varres`cond'*IMFliq
}
* propensity for country to participate
egen varIMF55=mean(IMF55) if year>=1980 & year<=2017, by(cid)
gen intIMF55=varIMF55*IMFliq
xtset cid year
foreach var in gini_disp lngdppc lngdppc2 trade fdi demo2 dratio urban cab IMFliq IMF55 BA2TOT SCsBA2 QCsBA2 BA2DEB BA2EXT BA2FIN BA2RFP BA2ENT BA2LAB BA2POV BA2GOV {
	gen L`var' = l.`var'
}
* EVs
gen LIMFadj = l.IMFadjustment
gen DLIMFadj = (LIMFadj < .)
gen DIMFadj = (IMFadjustment < .)
egen mean_IMFadj = mean(IMFadjustment)
egen sd_IMFadj = sd(IMFadjustment)
gen z_IMFadj = (IMFadjustment-mean_IMFadj)/sd_IMFadj
gen out_IMFadj = 1 if z_IMFadj>3 & !missing(z_IMFadj)
replace out_IMFadj = 1 if z_IMFadj<-3 & !missing(z_IMFadj)
egen IMFother = sum(IMFnn), by(year)
gen lnIMFliq = ln(IMFliquidity)
gen IMFtemp = IMFadjustment
replace IMFtemp = 0 if missing(IMFadjustment)
egen varIMFadj = mean(IMFtemp) if year>=2002 & year<=2017, by(cid)
gen intIMFadj = varIMFadj*lnIMFliq
gen IMFadjB = IMFadjustment*100
* ==============================================================================
* Factual statements
* ==============================================================================
* "The sample includes observations for the period 1980 to 2017, covering 4857 country-years across 194 countries, for an average of 25 years per country."
xtsum gini_disp if year>=1980 & year<=2017
* "For example, Vanuatu has data available for only five years, whereas 40 countries—such as Chile, Tanzania, and the United States—have values recorded for all 38 years."
egen gini_disp_freq = count(gini_disp) if year>=1980 & year<=2017, by(cname)
sort gini_disp_freq
list cname gini_disp_freq if year==2010
* "Setting the number of binding conditions at the mean, at 21.511 per year, would therefore result in an increase of 0.280 percentage points."
tabstat LBA2TOT if year>=1980 & year<=2017 & LIMF55==1, s(n mean sd mi ma) col(s)
* "In total, the dataset contains a maximum of 332 observations across 77 countries between 2002 and 2017.""
xtsum IMFadjB if year>=2002 & year<=2017
* "It shows these values are concentrated close to zero, and that these targets do not always require fiscal consolidation: 194 (58.4%) are austere, requiring the headline fiscal balance to increase, while 138 (41.6%) of them offer leniency for the headline fiscal balance to fall."
count if IMFadjB>0 & (year>=2002 & year<=2017) & !missing(IMFadjB)
count if IMFadjB<0 & (year>=2002 & year<=2017) & !missing(IMFadjB)
* ==============================================================================
* Table 5.2. Descriptive statistics for the income Gini, 1980-2017
* ==============================================================================
tabstat gini_disp if year>=1980 & year<=2017, s(n mean sd mi ma) col(s)
* ==============================================================================
* Table 5.3. Descriptive statistics and sources for control variables, 1980-2017
* ==============================================================================
tabstat Llngdppc Llngdppc2 Ltrade Lfdi Ldemo2 Ldratio if year>=1980 & year<=2017, s(n mean sd) col(s)
* ==============================================================================
* Table 5.4. Effect of IMF conditionality on income Gini, 1980-2017
* ==============================================================================
xtset cid year
cmp setup
local DV "gini_disp"
local ctrl "l.\`DV' l.lngdppc l.lngdppc2 l.trade l.fdi l.demo2 l.dratio i.cid ib2000.year" 
local sele "\`ctrl' l.cab"
* controls only
eststo: reg `DV' `ctrl' if year>=1980 & year<=2017
* binding conditions
eststo: cmp (`DV' = LIMF55 l.BA2TOT `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2TOT = l.intBA2TOT `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* quantitative vs structural conditions
eststo: cmp (`DV' = LIMF55 l.SCsBA2 l.QCsBA2 `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.SCsBA2 = l.intSCsBA2 `sele') (res: l.QCsBA2 = l.intQCsBA2 `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
esttab, b(4) se(4) nogaps brackets star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars(N_clust) sfmt(a4) drop(*cid *year)
estimates dir
estimates restore est2
test ([cond] l.intBA2TOT)
test ([LIMF55] l.intIMF55)
estimates restore est3
test ([cond] l.intSCsBA2)
test ([res] l.intQCsBA2)
test ([LIMF55] l.intIMF55)
eststo clear
* ==============================================================================
* Figure 5.1. Income Gini, 1980, 1990, 2010 and 2017
* ==============================================================================
preserve
*** get the shapefiles in order
capture cd "GIS"
spshape2dta "World_Countries__Generalized_.shp", replace saving(world)
*** explore the outline file
use world_shp, clear
geo2xy _Y _X, proj (web_mercator) replace
*** merge with attributes file to get the names
merge m:1 _ID using world
drop if COUNTRY=="Antarctica"
drop rec_header- _merge
sort _ID
save, replace
*** merging
use world, clear
ren COUNTRY cname             
drop if cname=="Antarctica"
replace cname="Bahamas, The" if cname=="Bahamas"
replace cname="Congo, Dem. Rep." if cname=="Congo DRC"
replace cname="Congo, Rep." if cname=="Congo"
replace cname="Cote d'Ivoire" if cname=="Côte d'Ivoire"
replace cname="Egypt, Arab Rep." if cname=="Egypt"
replace cname="Gambia, The" if cname=="Gambia"
replace cname="Iran, Islamic Rep." if cname=="Iran"
replace cname="Korea, Dem. People's Rep." if cname=="North Korea"
replace cname="Korea, Rep." if cname=="South Korea"
replace cname="Kyrgyz Republic" if cname=="Kyrgyzstan"
replace cname="Lao PDR" if cname=="Laos"
replace cname="Micronesia, Fed. Sts." if cname=="Micronesia"
replace cname="Sint Maarten (Dutch part)" if cname=="Sint Maarten"
replace cname="Slovak Republic" if cname=="Slovakia"
replace cname="St. Kitts and Nevis" if cname=="Saint Kitts and Nevis"
replace cname="St. Lucia" if cname=="Saint Lucia"
replace cname="St. Martin (French part)" if cname=="Saint Martin"
replace cname="St. Vincent and the Grenadines" if cname=="Saint Vincent and the Grenadines"
replace cname="Syrian Arab Republic" if cname=="Syria"
replace cname="Venezuela, RB" if cname=="Venezuela"
replace cname="Virgin Islands (U.S.)" if cname=="US Virgin Islands"
replace cname="West Bank and Gaza" if cname=="Palestinian Territory"
replace cname="Yemen, Rep." if cname=="Yemen"
merge 1:m cname using "..\IMFBOOK_Dataset.dta"
*** see which countries didn't merge
tab cname if _m==1 
tab cname if _m==2 
*** generate a local for the color scheme
colorpalette spmap greys, n(4) nograph
local colors `r(p)'
spmap gini_disp_SOLT using world_shp if year==1980, id(_ID) ocolor(black ..) osize(vvthin ..) fcolor("`colors'") legend(pos(7) size(*1)) legtitle("Income Gini (%) 1980") legstyle(2) ndfcolor(white ..) ndlabel("Missing") ndsize(vvthin ..) clmethod(custom) clbreaks(10 30 40 50 70)
graph export "..\Figures\IMFBOOK_Figure5-1ai.png", replace width(4000)
spmap gini_disp_SOLT using world_shp if year==1990, id(_ID) ocolor(black ..) osize(vvthin ..) fcolor("`colors'") legend(pos(7) size(*1)) legtitle("Income Gini (%) 1990") legstyle(2) ndfcolor(white ..) ndlabel("Missing") ndsize(vvthin ..) clmethod(custom) clbreaks(10 30 40 50 70)
graph export "..\Figures\IMFBOOK_Figure5-1aii.png", replace width(4000)
spmap gini_disp_SOLT using world_shp if year==2010, id(_ID) ocolor(black ..) osize(vvthin ..) fcolor("`colors'") legend(pos(7) size(*1)) legtitle("Income Gini (%) 2010") legstyle(2) ndfcolor(white ..) ndlabel("Missing") ndsize(vvthin ..) clmethod(custom) clbreaks(10 30 40 50 70)
graph export "..\Figures\IMFBOOK_Figure5-1bi.png", replace width(4000)
spmap gini_disp_SOLT using world_shp if year==2017, id(_ID) ocolor(black ..) osize(vvthin ..) fcolor("`colors'") legend(pos(7) size(*1)) legtitle("Income Gini (%) 2017") legstyle(2) ndfcolor(white ..) ndlabel("Missing") ndsize(vvthin ..) clmethod(custom) clbreaks(10 30 40 50 70)
graph export "..\Figures\IMFBOOK_Figure5-1bii.png", replace width(4000)
capture cd ..
restore
* ==============================================================================
* Figure 5.2. Income Gini, 1980-2017
* ==============================================================================
preserve
egen expBA=mean(BA2TOT) if year>=1980 & year<=2017, by(cid)
sum expBA if year>=1980 & year<=2017, d
gen highBA=(expBA>`r(p50)') if year>=1980 & year<=2017
egen meangini_disp = mean(gini_disp) if year>=1980 & year<=2017, by(year)
egen highBAmean = mean(gini_disp) if year>=1980 & year<=2017 & highBA==1, by(year)
egen lowBAmean = mean(gini_disp) if year>=1980 & year<=2017 & highBA==0, by(year)
twoway (scatter meangini_disp year if year>=1980 & year<=2017, sort(year) connect(l) msymbol(i) lp(solid) lw(thick)) (scatter highBAmean year if year>=1980 & year<=2017, sort(year) connect(l) msymbol(i) lp(dash) lw(thick)) (scatter lowBAmean year if year>=1980 & year<=2017, sort(year) connect(l) msymbol(i) lp(shortdash_dot) lw(thick)), graphregion(color(white)) legend(rows(3) label(1 "Mean of all countries") label(2 "Above-mean conditionality countries") label(3 "Below-mean conditionality countries")) ytitle("Income Gini (%)") ylabel(, angle(horizontal)) xlabel(1980(10)2020) xline(1990 2010, lcolor(black) lwidth(thin) lpattern(dash)) scheme(s2mono)
graph export "Figures\IMFBOOK_Figure5-2.png", replace width(4000)
restore
* ==============================================================================
* Figure 5.3. Association between average annual income Gini and total number of binding conditions, 1980-2017
* ==============================================================================
egen totalBA2TOT = total(BA2TOT) if year>=1980 & year<=2017, by(cid)
egen meangini_disp = mean(gini_disp) if year>=1980 & year<=2017, by(cid)
corr totalBA2TOT meangini_disp if year>=1980 & year<=2017
local r: display %5.2f r(rho)
twoway (scatter meangini_disp totalBA2TOT if year>=1980 & year<=2017, msymbol(i) mlab(ccode) mlabpos(0)) (lfit meangini_disp totalBA2TOT if year>=1980 & year<=2017), legend(off) ytitle("Income Gini (%)") xtitle("Total number of conditions") ylabel(, angle(0)) note(r = `r', size(medsmall) placement(se)) graphregion(color(white)) scheme(s2mono)
graph export "Figures\IMFBOOK_Figure5-3.png", replace width(4000)
* ==============================================================================
* Figure 5.4. Effect of IMF conditionality on income Gini, with 95% and 90% confidence intervals
* ==============================================================================
xtset cid year
cmp setup
local DV "gini_disp"
local ctrl "l.\`DV' l.lngdppc l.lngdppc2 l.trade l.fdi l.demo2 l.dratio i.cid ib2000.year" 
local sele "\`ctrl' l.cab"
cmp (gini_disp = LIMF55 l.BA2TOT `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2TOT = l.intBA2TOT `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
margins, at(l.BA2TOT=(0 10 20 30 40 50) LIMF55=1) vsquish level(95) noestimcheck saving(l95, replace)
margins, at(l.BA2TOT=(0 10 20 30 40 50) LIMF55=1) vsquish level(90) noestimcheck saving(l90, replace)
combomarginsplot l95 l90, recast(line) recastci(rarea) ytitle(Linear prediction: Income Gini (%)) ylabel(, angle(horizontal)) xtitle(Number of binding conditions) title("") graphregion(color(white)) scheme(s2mono) legend(off)
graph export "Figures\IMFBOOK_Figure5-4.png", replace width(4000)
erase l95.dta
erase l90.dta
* ==============================================================================
* Figure 5.5. Effect of IMF conditionality policy areas on income Gini, with 95% and 90% confidence intervals
* ==============================================================================
xtset cid year
cmp setup
local DV "gini_disp"
local ctrl "l.\`DV' l.lngdppc l.lngdppc2 l.trade l.fdi l.demo2 l.dratio i.cid ib2000.year" 
local sele "\`ctrl' l.cab"
* DEB
eststo: cmp (`DV' = LIMF55 l.BA2DEB l.resBA2DEB `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2DEB = l.intBA2DEB `sele') (res: l.resBA2DEB = l.intresBA2DEB `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* FIN
eststo: cmp (`DV' = LIMF55 l.BA2FIN l.resBA2FIN `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2FIN = l.intBA2FIN `sele') (res: l.resBA2FIN = l.intresBA2FIN `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* RFP
eststo: cmp (`DV' = LIMF55 l.BA2RFP l.resBA2RFP `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2RFP = l.intBA2RFP `sele') (res: l.resBA2RFP = l.intresBA2RFP `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* EXT
eststo: cmp (`DV' = LIMF55 l.BA2EXT l.resBA2EXT `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2EXT = l.intBA2EXT `sele') (res: l.resBA2EXT = l.intresBA2EXT `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* ENT 	
eststo: cmp (`DV' = LIMF55 l.BA2ENT l.resBA2ENT `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2ENT = l.intBA2ENT `sele') (res: l.resBA2ENT = l.intresBA2ENT `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* LAB
eststo: cmp (`DV' = LIMF55 l.BA2LAB l.resBA2LAB `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2LAB = l.intBA2LAB `sele') (res: l.resBA2LAB = l.intresBA2LAB `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* GOV
eststo: cmp (`DV' = LIMF55 l.BA2GOV l.resBA2GOV `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2GOV = l.intBA2GOV `sele') (res: l.resBA2GOV = l.intresBA2GOV `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
* POV
eststo: cmp (`DV' = LIMF55 l.BA2POV l.resBA2POV `ctrl') (LIMF55 = l.intIMF55 `sele') (cond: l.BA2POV = l.intBA2POV `sele') (res: l.resBA2POV = l.intresBA2POV `sele') if year>=1980 & year<=2017, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont) cl(cid) iterate(50) difficult
esttab, b(4) se(4) nogaps brackets star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars(N_clust) sfmt(a4) drop(*cid *year)
estimates dir
estimates restore est1
test ([cond] l.intBA2DEB)
test ([res] l.intresBA2DEB)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2DEB) noestimcheck level(90)
estimates restore est2
test ([cond] l.intBA2FIN)
test ([res] l.intresBA2FIN)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2FIN) noestimcheck level(90)
estimates restore est3
test ([cond] l.intBA2RFP)
test ([res] l.intresBA2RFP)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2RFP) noestimcheck level(90)
estimates restore est4
test ([cond] l.intBA2EXT)
test ([res] l.intresBA2EXT)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2EXT) noestimcheck level(90)
estimates restore est5
test ([cond] l.intBA2ENT)
test ([res] l.intresBA2ENT)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2ENT) noestimcheck level(90)
estimates restore est6
test ([cond] l.intBA2LAB)
test ([res] l.intresBA2LAB)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2LAB) noestimcheck level(90)
estimates restore est7
test ([cond] l.intBA2GOV)
test ([res] l.intresBA2GOV)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2GOV) noestimcheck level(90)
estimates restore est8
test ([cond] l.intBA2POV)
test ([res] l.intresBA2POV)
test ([LIMF55] l.intIMF55)
margins, dydx(l.BA2POV) noestimcheck level(90)
coefplot (est1, offset(0)) est2 est3 est4 est5 est6 est7, keep(`DV':L.BA2*) coeflabels(L.BA2DEB="External debt issues" L.BA2FIN=`""Financial, monetary" "& Central Bank"' L.BA2RFP=`""Fiscal issues," "revenues & taxation""' L.BA2EXT=`""External sector" "(trade & exchange)"' L.BA2ENT=`""SOE privatisation," "reform & pricing""' L.BA2LAB=`""Labour (public" "& private sector)""' L.BA2GOV="Institutional reforms") mcolor(black) ciopts(recast(. rcap) lcolor(black black)) xline(0, lcolor(black) lwidth(thin) lpattern(dash)) xtitle("Conditionality effect size") levels(95 90) nokey grid(within) graphregion(color(white))
graph export "Figures\IMFBOOK_Figure5-5.png", replace width(4000)
eststo clear
* ==============================================================================
* Figure 5.6. Distribution of IMF fiscal adjustment, 2002-2017 
* ==============================================================================
histogram IMFadjB if year>=2002 & year<=2017, frequency graphregion(color(white)) ylabel(0(20)140, angle(horizontal)) xlabel(-10(5)30) xtitle("IMF fiscal adjustment (% GDP)") scheme(s2mono)
graph export "Figures\IMFBOOK_Figure5-6.png", replace width(4000)
* ==============================================================================
* Figure 7. Effect of IMF fiscal adjustment on share of income decile, with 95% and 90% confidence intervals
* ==============================================================================
xtset cid year
cmp setup
foreach DV of varlist incdec1-incdec10 {
	local ctrl "l.\`DV' l.lngdppc l.lngdppc2 l.trade l.fdi l.demo2 l.dratio"
	local sele "\`ctrl' l.cab"
	eststo: heckman `DV' LIMFadj `ctrl' i.cid i.year if year>=2002 & year<=2017, select(DLIMFadj = `sele' l.intIMFadj i.year) twostep first
}
esttab, b(4) se(4) nogaps brackets star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars(N_selected) sfmt(a4) drop(*cid *year)
estimates dir
local estno `r(names)'
foreach eq of local estno {
	estimates restore `eq'
	margins, dydx(LIMFadj) level(90)
}
coefplot est1|| est2 || est3 || est4 || est5 || est6 || est7 || est8 || est9 || est10, keep(LIMFadj) bycoefs ytitle("Income decile") bylabels("1" "2" "3" "4" "5" "6" "7" "8" "9" "10") mcolor(black) ciopts(recast(. rcap) lcolor(black black)) xline(0, lcolor(black) lwidth(thin) lpattern(dash)) levels(95 90) xtitle("IMF adjustment effect size") graphregion(color(white))
graph export "Figures\IMFBOOK_Figure5-7.png", replace width(4000)
eststo clear
* ==============================================================================
